<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const resolve = ()=>{
            console.log(1)
        }

        setTimeout(() => {
            resolve()
        }, 1000);

        setTimeout(resolve, 1000)

        function delay(s){
            return new Promise((resolve, reject) => {
                setTimeout(()=>{
                    resolve()
                }, s * 1000)
            })
        }

        // console.log(delay(1))

        // const sleep = ms => {
        //     return new Promise((resolve) => {
        //         setTimeout(() => {
        //             resolve()
        //         }, ms)
        //     })
        // }

        // 延迟某段时间执行某段代码
        const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

        // console.log(sleep(1000))
        sleep(1000).then(() => {
            console.log(1)
            return sleep(2000)
        }).then(() => {
            console.log(2)
        })


        // ==> 用异步的方法去实现了同步的功能。
    </script>
</body>
</html>